#include "br_com_paysmart_experiments_jni_JNITest.h"

#include <cstring>


class AbstractSummer
{
public:
	virtual ~AbstractSummer() {}
	virtual int sum(int a, int b) = 0;
};

class Summer : public AbstractSummer
{
	~Summer() {}
	virtual int sum(int a, int b)
	{
		return a+b;
	}
};

class AbstractMemCopier
{
public:
	virtual ~AbstractMemCopier() {}
	virtual void MemCopier(int* buffer, int iValue, int iSize) = 0;
};

class SlowMemCopier : public AbstractMemCopier
{
public:
	~SlowMemCopier() {}
	virtual void MemCopier(int* buffer, int iValue, int iSize)
	{
		memset(buffer, iValue, sizeof(int)*iSize);
		for (int k=0; k<iSize; k++)
			buffer[k] = iValue;
	}
};



JNIEXPORT jint JNICALL Java_br_com_paysmart_experiments_jni_JNITest_Native_1DoLoopedWork
  (JNIEnv *, jobject, jint iInputValue, jint iLoopCount)
{
	long iSum = 0;
	for (long j=0; j<iLoopCount; j++)
	{
		iSum = 0;
		int *copy = new int[iInputValue+1];
		AbstractSummer *summer = new Summer();
		AbstractMemCopier *copier = new SlowMemCopier();
		for (long i=1; i<=iInputValue; i++)
		{			
			copier->MemCopier(copy, i, i);	
			iSum = summer->sum(iSum, copy[i-1]);
		}
		delete copier;
		delete summer;
		delete []copy;
	}
	return iSum;
}